﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CommonLib.Pattern;
public class EPChainNodeAttackOther : EPChainNodeNone
{
	//float SteeringParam = 0.01f;
	protected override bool TryDeal(object o)
	{
		RoleCtrl role = o as RoleCtrl;
		if (FindOthers(role, role.transform.position, new Vector2(10, 10), out Collider2D col))
		{
			Vector2 steering = new Vector2(col.transform.position.x - role.transform.position.x,
				col.transform.position.y - role.transform.position.y).normalized;

			Vector2 desired = (role.SubManagerMove.VelocityDir + steering) / 2;
			//role.SubManagerMove.Rb2d.velocity = desired.normalized * role.RoleEntity.CurrentSpeed;
			role.SubManagerMove.SetVelocityDir(desired);
			Debug.Log("EPChainNodeAttackOther");
			return true;
		}
		return false;
	}

	protected virtual bool FindOthers(RoleCtrl @object, Vector2 center, Vector2 size, out Collider2D collider)
	{
		collider = null;
		float length = 999;
		Collider2D[] collider2D = Physics2D.OverlapBoxAll(center, size, 0);
		if (collider2D.Length <= 1)
			return false;
		else
		{

			for (int i = 0; i < collider2D.Length; i++)
			{
				if (collider2D[i].gameObject.tag == "Player" && collider2D[i].gameObject != @object.gameObject)
				{
					if(@object.RoleEntity.CurrentScore > collider2D[i].gameObject.GetComponent<RoleCtrl>().RoleEntity.CurrentScore)
					{
						if(Vector3.Distance(@object.transform.position, collider2D[i].transform.position)< length)
						{
							collider = collider2D[i];
						}
					}
					//return true;
				}
			}
			if (null != collider) return true;
		}
		// Physics.OverlapBox(transform.position, size, Quaternion.identity)
		return false;
	}
}
